'''
Author: xialang333@gmail.com
Date: 2024-08-19 15:31:39
LastEditTime: 2024-08-19 17:07:17
# 修改devops_files表

'''
import re

from common import ConnDB, CustomLogger

# 旧host、domain 关键字
host = "10.2.16.238"
domain = "dgyh"
# 新host、domain
new_host = "10.0.172.57"
new_domain = "xlnas"


# 创建并配置日志记录器
logger = CustomLogger('devops_files').get_logger()

class Main:
    def __init__(self):
        self.sql = "select * from devops_files"
        self.sql_update = f"UPDATE devops_files SET content = %s WHERE id = %s"
        
    def run(self):
        conn = ConnDB()
        conn.connect()
        result = conn.fetch_all(self.sql)
        counter = 0
        for row in result:
            content = row[3]
            if host in content and domain in content:
                new_content_temp = re.sub(host, new_host, content)
                new_content = re.sub(domain, new_domain, new_content_temp)
                counter += 1
                conn.execute_query(self.sql_update,(new_content, row[0]))
                logger.info(f"更新devops_files表数据id: {row[0]}")
            elif host in content and domain not in content:
                new_content = re.sub(host, new_host, content)
                conn.execute_query(self.sql_update,(new_content, row[0]))
                counter += 1
                logger.info(f"更新devops_files表数据id: {row[0]}")
            elif host not in content and domain in content:
                new_content = re.sub(domain, new_domain, content)
                conn.execute_query(self.sql_update,(new_content, row[0]))
                counter += 1
                logger.info(f"更新devops_files表数据id: {row[0]}")
            else:
                pass
            
        conn.close()
        logger.info(f"devops_files表数据修改完成: 共计{counter}条")

if __name__ == "__main__":
    tt = Main()
    tt.run()
    